<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="$t('添加备份配置的描述')" :v-if="dialogVisible" :visible.sync="dialogVisible" width="600px" @close='close'>
    <el-form ref="form" :model="record" label-width="100px" :rules="rules">
      <el-form-item :label="$t('描述')" prop="desc">
        <el-input v-emoji v-model="record.desc" ref="recordDesc" style="width:350px;" :autosize="{ minRows: 4 }" type="textarea" @keydown.native="handleKeyCode($event)"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="addSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="addClose">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>

import { backupConfig } from '@/api/system/configManager'
import { texttextClass } from '@/utils/input-class.js'
export default {
  name: 'BackupsDialog',
  data () {
    // var validateDesc = (rule, value, callback) => {
    //   if (!String(value).trim()) {
    //     callback(new Error(this.$t('请输入描述')))
    //   } else {
    //     callback()
    //   }
    // }
    return {
      dialogVisible: false,
      record: {
        desc: ''
      },
      recordCopy: {
        desc: ''
      },
      rules: {
        // desc: [{ required: false, validator: validateDesc, trigger: 'blur' }]
      }
    }
  },
  watch: {
    'record.desc': {
      handler (newVal) {
        this.record.desc = newVal.replace(/[\r\n]/g, '')
      },
      immediate: true
    }
  },
  methods: {
    init (data) {
      this.dialogVisible = true
      this.$nextTick(() => {
        const textdata = ['recordDesc']
        texttextClass(this.$refs, textdata)
      })
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          backupConfig(this.record).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('添加成功'),
                type: 'success'
              })
              this.close()
              this.$parent.listData()
            }
          })
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.record = Object.assign({}, this.recordCopy)
      this.$refs.form.resetFields()
    },
    handleKeyCode (event) {
      if (event.keyCode === 13) {
        event.preventDefault() // 阻止浏览器默认换行操作
        return false
      }
    }
  }
}
</script>
